package com.hunan.api.catv.service.biz.album;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.ColumnEnum;
import com.hunan.api.catv.entity.po.Album;
import com.hunan.api.catv.entity.vo.AlbumVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.tag_content.TagContentBiz;
import com.hunan.api.catv.service.impl.album.AlbumService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@CacheConfig(cacheNames = CacheName.album)
public class AlbumBiz extends ResultService {

    @Autowired
    AlbumService albumService;

    @Autowired
    AlbumBiz albumBiz;

    @Autowired
    TagContentBiz tagContentBiz;

    @Autowired
    CacheBiz cacheBiz;

    public ServiceResult add(Album album) {
        ServiceResult result = albumService.add(album);
        if (result.getStatus()) {
            result = tagContentBiz.add(album.getTagIds(), album.getAlbumId(), ColumnEnum.video.getCode(), album.getColumnId());
            if (result.getStatus()) {
                return buildOK(true, ConstantsMsg.admin_add_success);
            }
        }
        return buildFail(false, ConstantsMsg.admin_add_fail);
    }

    public ServiceResult del(Integer albumId, Integer state) {
        Album album = new Album();
        album.setAlbumId(albumId);
        album.setState(state);
        ServiceResult result = albumService.update(album);
        if (result.getStatus()) {
            String simpleInfoKey = MessageFormat.format(CacheName.albumSimpleInfo, String.valueOf(albumId));
            String infoKey = MessageFormat.format(CacheName.albumInfo, String.valueOf(albumId));
            String beanNames = MessageFormat.format("albumSimpleInfoCache?{0};albumInfoCache?{1}", String.valueOf(albumId), String.valueOf(albumId));
            cacheBiz.flushCacheInfo(beanNames, simpleInfoKey, infoKey);
            return buildOK(true, ConstantsMsg.admin_del_success);
        }
        return buildFail(false, ConstantsMsg.admin_del_fail);
    }

    public ServiceResult edit(Album album) {
        ServiceResult result = albumService.update(album);
        if (result.getStatus()) {
            result = tagContentBiz.edit(album.getTagIds(), album.getAlbumId(), ColumnEnum.video.getCode(), album.getColumnId());
            if (result.getStatus()) {
                String simpleInfoKey = MessageFormat.format(CacheName.albumSimpleInfo, String.valueOf(album.getAlbumId()));
                String infoKey = MessageFormat.format(CacheName.albumInfo, String.valueOf(album.getAlbumId()));
                String beanNames = MessageFormat.format("albumSimpleInfoCache?{0};albumInfoCache?{1}", String.valueOf(album.getAlbumId()), String.valueOf(album.getAlbumId()));
                cacheBiz.flushCacheInfo(beanNames, simpleInfoKey, infoKey);
                return buildOK(true, ConstantsMsg.admin_edit_success);
            }
        }
        return buildFail(false, ConstantsMsg.admin_edit_fail);
    }

    public PageInfo<AlbumVo> adminPage(Integer pageNum, Integer pageSize, Integer columnId, Integer videoState, String title) {
        PageHelper.startPage(pageNum, pageSize);
        Album album = new Album();
        album.setColumnId(columnId);
        album.setVideoState(videoState);
        album.setName(title);
        List<AlbumVo> list = albumService.list(album);
        return new PageInfo<AlbumVo>(list);
    }

    public AlbumVo getById(Integer albumId, Integer columnId) {
        AlbumVo albumVo = albumService.getById(albumId);
        String tagIds = tagContentBiz.getTagIds(albumId, ColumnEnum.video.getCode(), columnId);
        albumVo.setTagIds(tagIds);
        return albumVo;
    }

    @Cacheable(unless = "#result == null")
    public AlbumVo simpleInfo(Integer albumId) {
        return albumService.simpleInfo(albumId);
    }

    @Cacheable(unless = "#result == null")
    public AlbumVo info(Integer albumId) {
        return albumService.getById(albumId);
    }

    @Cacheable(unless = "#result == null")
    public PageInfo<AlbumVo> albumList(Integer columnId, int pageNum, int pageSize) {
        PageInfo<Integer> idsPage = albumBiz.idsList(columnId, pageNum, pageSize);
        List<Integer> ids = idsPage.getList();
        List<AlbumVo> albumList = new ArrayList<AlbumVo>();
        ids.forEach(id -> {
            AlbumVo albumVo = albumBiz.simpleInfo(id);
            if (!ObjectUtils.isEmpty(albumVo)) {
                albumList.add(albumVo);
            }
        });
        PageInfo<AlbumVo> pageInfo = new PageInfo<AlbumVo>();
        BeanUtils.copyProperties(idsPage, pageInfo);
        pageInfo.setList(albumList);
        return pageInfo;
    }

    @Cacheable(unless = "#result == null")
    public PageInfo<Integer> idsList(Integer columnId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Album album = new Album();
        album.setColumnId(columnId);
        album.setState(1);
        List<Integer> list = albumService.listIds(album);
        return new PageInfo<>(list);
    }
}
